//create a scope so we don't pollute global
(function() {

    var currentTestName;
    var currentModuleName;
    var currentModuleStart;
    var currentTestStart;

    var moduleReports = {
        modules: {},
        count: 0,
        failures: 0,
        time: 0
    };

    window.getCoverage = function() {

        var report = {};

        if (_$jscoverage) for (var file in _$jscoverage) {
            if (!_$jscoverage.hasOwnProperty(file)) continue;

            var coverage = _$jscoverage[file];

            report[file] = {};
            report[file].source = coverage.source;
            report[file].file = file;
            report[file].coverage = {};
            for (var i = 0; i < coverage.source.length; i++) {
                if (coverage[i] != undefined) {
                    report[file].coverage[i] = coverage[i];
                }
            }

        }

        return JSON['stringify' in JSON ? 'stringify' : 'encode']({
            coverage: report,
            report: moduleReports
        });

    };

    //arg: { name }
    QUnit.moduleStart = function(t) {
        currentModuleName = t.name;
        currentModuleStart = new Date();
        console.log('Module "' + t.name + '"');
        moduleReports.modules[currentModuleName] = {
            tests: {},
            time: 0,
            count: 0,
            failures: 0
        };
    };

    //arg: { name }
    QUnit.moduleDone = function(t) {
        console.log('Module "' + t.name + '" completed: ' + (0 === t.failed ? 'pass' : 'FAIL'));
        console.log('');
        moduleReports.modules[currentModuleName].time = (new Date()).getTime() - currentModuleStart.getTime();
    };

    //arg: { name }
    QUnit.testStart = function(t) {
        currentTestName = t.name;
        currentTestStart = new Date();
        moduleReports.modules[currentModuleName].tests[currentTestName] = {
            time: 0,
            success: false
        };
    };

    //arg: { name, failed, passed, total }
    QUnit.testDone = function(t) {
        console.log('Test "' + t.name + '" completed: ' + (0 === t.failed ? 'pass' : 'FAIL'));
        moduleReports.modules[currentModuleName].tests[currentTestName].time = (new Date()).getTime() - currentTestStart.getTime();
        moduleReports.modules[currentModuleName].tests[currentTestName].success = t.failed;
        moduleReports.modules[currentModuleName].time += moduleReports.modules[currentModuleName].tests[currentTestName].time;
        moduleReports.modules[currentModuleName].count++;
        moduleReports.modules[currentModuleName].failures += (t.failed ? 1 : 0);
        moduleReports.time += moduleReports.modules[currentModuleName].tests[currentTestName].time;
        moduleReports.count++;
        moduleReports.failures += (t.failed ? 1 : 0);
    };

    //{ result, actual, expected, message }
    QUnit.log = function(t) {
        if (!t.result) {
            console.log('Test "' + currentTestName + '" assertion failed. Expected <' + t.expected + '> Actual <' + t.actual + '>' + (t.message ? ': \'' + t.message + '\'' : ''));
        }
    };

    QUnit.done = function(t) {
    };

}());